﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XUtility
{
    public static class ResponseClass
    {
        #region 响应结果
        /// <summary>
        /// 返回请求处理结果信息
        /// </summary>
        /// <param name="flag">请求处理结果</param>
        /// <returns>结果信息</returns>
        public static String ResponseResult(Boolean flag)
        {
            return ResponseResult(flag, null, null, null);
        }
        /// <summary>
        /// 返回请求处理结果信息
        /// </summary>
        /// <param name="flag">请求处理结果</param>
        /// <param name="msg">信息</param>
        /// <returns>结果信息</returns>
        public static String ResponseResult(Boolean flag, String msg)
        {
            return ResponseResult(flag, msg, null);
        }
        /// <summary>
        /// 返回请求处理结果信息
        /// </summary>
        /// <param name="flag">请求处理结果</param>
        /// <param name="msg">信息</param>
        /// <param name="customData">自定义数据</param>
        /// <returns>结果信息</returns>
        public static String ResponseResult(Boolean flag, String msg, String customData)
        {
            String okMsg = msg;
            String noMsg = msg;

            return ResponseResult(flag, okMsg, noMsg, customData);
        }
        /// <summary>
        /// 返回请求处理结果信息
        /// </summary>
        /// <param name="flag">请求处理结果</param>
        /// <param name="okMsgInfo">成功信息 [信息;图标] 结束符分割</param>
        /// <param name="noMsgInfo">失败信息 [信息;图标] 结束符分割</param>
        /// <param name="customData">自定义数据</param>
        /// <returns>结果信息</returns>
        public static String ResponseResult(Boolean flag, String okMsgInfo, String noMsgInfo, String customData)
        {
            String msg = String.Empty;
            String icon = String.Empty;

            if (flag)
            {
                msg = "操作成功！";
                icon = "info";

                if (okMsgInfo.HasValue())
                {
                    String[] tempOK = okMsgInfo.Split(ResponseIcon.SP);
                    Int32 okCount = tempOK.Length;
                    if (okCount > 0)
                    {
                        msg = tempOK[0];
                        if (okCount > 1)
                        {
                            icon = tempOK[1];
                        }
                    }
                }
            }
            else
            {
                msg = "操作失败！";
                icon = "error";

                if (noMsgInfo.HasValue())
                {
                    String[] tempNO = noMsgInfo.Split(ResponseIcon.SP);
                    Int32 noCount = tempNO.Length;
                    if (noCount > 0)
                    {
                        msg = tempNO[0];
                        if (noCount > 1)
                        {
                            icon = tempNO[1];
                        }
                    }
                }
            }

            return ("{\"success\":" + flag.ToLowerString() + ",\"msg\":\"" + msg + "\",\"icon\":\"" + icon + "\"" + customData + "}");
        }
        #endregion

        #region 响应 Alert 消息类型 默认Easyui格式
        /// <summary>
        ///  响应 Alert 消息类型 默认Easyui格式
        /// </summary>
        public struct ResponseIcon
        {
            /// <summary>
            /// 消息与类型 分隔符
            /// </summary>
            internal const Char SP = ';';
            /// <summary>
            /// 信息
            /// </summary>
            public readonly static String INFO = SP + "info";
            /// <summary>
            /// 错误
            /// </summary>
            public readonly static String ERROR = SP + "error";
            /// <summary>
            /// 问题
            /// </summary>
            public readonly static String QUESTION = SP + "question";
            /// <summary>
            /// 警告
            /// </summary>
            public readonly static String WARNING = SP + "warning";
        }
        #endregion

        #region 错误结果
        /// <summary>
        /// 错误结果
        /// </summary>
        /// <returns></returns>
        public static String ResponseError(String msg = null, String customData = null)
        {
            return ResponseResult(false, msg, customData);
        }
        #endregion

        #region 空数组 默认Easyui格式
        /// <summary>
        /// 空数组 默认Easyui格式
        /// </summary>
        /// <returns></returns>
        public static String ResponseEmpty()
        {
            return "{total:0,rows:[]}";
        }
        #endregion
    }
}
